load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRFUserARW.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRF_contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/contrib/ut_string.ncl"

begin
;    a = addfile("/public/users/wrf/wrfout/2012072300/wrfout_d02_2012-07-23_00:00:00.nc", "r")
;    output_prefix = "/public/users/wrf/wrfpost/grid_sswdn/2012072308/"
;    system("mkdir " + output_prefix)
    a = addfile(input_file + ".nc", "r")
    sswdn_dt = 1 ; surface sw down flux time interval

    ; read precipitation
    char_times = a->Times
    times = wrf_user_getvar(a, "Times", -1);
    hours = WRF_Times2Udunits_c(char_times, 0)
    wrf_dt = hours(1) - hours(0)
    hours = hours + 8
    times_cst = ut_string(hours, "%Y-%N-%D_%H:%M:%S")
    print(times_cst)
    ntimes = dimsizes(hours)

    sswdn = wrf_user_getvar(a, "SSWDN", -1);
    printVarSummary(sswdn)
    dim_sswdn = dimsizes(sswdn)
    ny = dim_sswdn(1)
    nx = dim_sswdn(2)
    print(dim_sswdn)

    dt = tointeger(sswdn_dt / wrf_dt + 0.1) ; time steps in WRFOUT
    print(dt)

    output_filecount = 1
    do it = 0, ntimes-1, dt
        timeStamp = ut_string(hours(it), "%Y%N%D%H")
        ; print(timeStamp)

        line_array = new(ny + 6, "string")
        line_count = 0

        ; write file header
        lonlat = wrf_user_ij_to_ll(a, 0+1, 0+1, True)
        line_array(line_count) = "xCorrdinate:" + sprintf("%8.4f", lonlat(0))
        line_count = line_count + 1
        line_array(line_count) = "yCorrdinate:" + sprintf("%8.4f", lonlat(1))
        line_count = line_count + 1
        line_array(line_count) = "numRows:" + sprinti("%i", ny)
        line_count = line_count + 1
        line_array(line_count) = "numColumns:" + sprinti("%i", nx)
        line_count = line_count + 1
        line_array(line_count) = "cellResolution:9"
        line_count = line_count + 1
        line_array(line_count) = "time:" + timeStamp
        line_count = line_count + 1

        ; write Radiation data
        do iy = 0, ny-1
            line = ""
            do ix = 0, nx-2
                line = line + sprintf("%5.1f", sswdn(it, iy, ix)) + ","
            end do
            line = line + sprintf("%5.1f", sswdn(it, iy, nx-1))
            line_array(line_count) = line
            line_count = line_count + 1
        end do

        output_filename = output_prefix + ut_string(hours(0), "%Y%N%D%H") + sprinti("%2i", tointeger(hours(ntimes-1) - hours(0))) + sprinti("%0.2i", output_filecount) + ".txt"
        asciiwrite(output_filename, line_array)
        output_filecount = output_filecount + 1
    end do

    output_lonlat = True
    if (output_lonlat .eq. True) then
        ; write Lat/Lon data
        lon_array = new(ny, "string")
        lat_array = new(ny, "string")
        line_count = 0

        do iy = 0, ny-1
            lon = ""
            lat = ""
            do ix = 0, nx-2
                lonlat = wrf_user_ij_to_ll(a, iy+1, ix+1, True)
                lon = lon + sprintf("%8.4f", lonlat(0)) + ","
                lat = lat + sprintf("%8.4f", lonlat(1)) + ","
            end do
            lonlat = wrf_user_ij_to_ll(a, iy+1, nx+1-1, True)
            lon = lon + sprintf("%8.4f", lonlat(0))
            lat = lat + sprintf("%8.4f", lonlat(1))
            lon_array(line_count) = lon
            lat_array(line_count) = lat
            line_count = line_count + 1
            print(line_count)
        end do
        asciiwrite(output_prefix + "lon.txt", lon_array)
        asciiwrite(output_prefix + "lat.txt", lat_array)

    end if
end
